package com.ttxz.sample.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.ttxz.common.holder.CommonRecyclerViewHolder
import com.ttxz.sample.bean.ArticleListItemBean
import com.ttxz.sample.databinding.ItemArticleListBinding

class ArticleListAdapter(val mdatas: MutableList<ArticleListItemBean>) :
    RecyclerView.Adapter<CommonRecyclerViewHolder<ItemArticleListBinding>>() {

    lateinit var onItemClick: (v: View, pos: Int, url:String, title:String) -> Unit

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CommonRecyclerViewHolder<ItemArticleListBinding> {

        val itemBinding = ItemArticleListBinding.inflate(LayoutInflater.from(parent.context), parent, false)

        return CommonRecyclerViewHolder(itemBinding)
    }

    override fun onBindViewHolder(holder: CommonRecyclerViewHolder<ItemArticleListBinding>, position: Int) {

        val data = mdatas[position]
        if (data.author.isEmpty()) {
            holder.binding.tvShareUserTitle.text = "分享人"
            holder.binding.tvShareUser.text = data.shareUser
        } else {
            holder.binding.tvShareUserTitle.text = "作者"
            holder.binding.tvShareUser.text = data.author
        }

        holder.binding.tvTitle.text = data.title
        holder.binding.tvSuperChapterName.text = data.superChapterName
        holder.binding.tvShareDate.text = data.niceShareDate
        holder.binding.root.setOnClickListener(){
            onItemClick.invoke(it, position, data.link, data.title)
        }
    }

    override fun getItemCount(): Int {
        return mdatas.size
    }
}